package net.sxjr.protal.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sxjr.common.*;
import net.sxjr.mbg.entity.IgRiskLinkManage;
import net.sxjr.mbg.entity.JbdpEmployee;
import net.sxjr.mbg.entity.JbdpUser;
import net.sxjr.mbg.entity.SysUploadFile;
import net.sxjr.mbg.mapper.IgRiskLinkManageMapper;
import net.sxjr.mbg.mapper.JbdpEmployeeMapper;
import net.sxjr.mbg.mapper.JbdpUserMapper;
import net.sxjr.mbg.mapper.SysUploadFileMapper;
import net.sxjr.protal.mapconverter.Converter;
import net.sxjr.protal.mapper.LoginMapper;
import net.sxjr.protal.user.dao.LoginUserInfo;
import net.sxjr.protal.user.dao.ModelRole;
import net.sxjr.protal.user.dao.UserInfo;
import net.sxjr.protal.user.params.CodeParams;
import net.sxjr.protal.user.params.LoginParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：cuixc
 * @date ：Created in 2020/12/10 17:17
 * @description：登入控制
 * @modified By：
 */
@Service
public class LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private JbdpUserMapper jbdpUserMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private JbdpEmployeeMapper employeeMapper;

    public Response<LoginUserInfo> login(LoginParams loginParam){
        //判断当前账户是否存在
        JbdpUser jbdpUser = jbdpUserMapper.selectOne(new LambdaQueryWrapper<JbdpUser>().eq(JbdpUser::getAccount, loginParam.getName()));
        if(jbdpUser == null){
            return Response.error(ErrorType.USERNOTFIND.getCode(),ErrorType.USERNOTFIND.getMsg());
        }
        if(!jbdpUser.getPassword().equals(MD5.Md5(loginParam.getPassword()))){
            return Response.error(ErrorType.passworderror.getCode(),ErrorType.passworderror.getMsg());
        }

        List<LoginUserInfo> users = loginMapper.getUserByAccount(loginParam.getName());
        LoginUserInfo user = null;

        if(users != null && users.size()>0){
            user = users.get(0);
            String roleName = users.stream().map(v -> v.getRoleName()).collect(Collectors.joining(","));
            String roleId = users.stream().map(v -> v.getRoleId()).collect(Collectors.joining(","));
            List<ModelRole> modelRoles = loginMapper.getModelRoleByAccount(loginParam.getName());
            modelRoles = modelRoles.stream().filter(val -> Arrays.asList(GlobalParam.menuList).contains(val.getVRNAME())).collect(Collectors.toList());
            user.setModelRoles(modelRoles);
            user.setRoleName(roleName);
            user.setRoleId(roleId);

            redisService.set(user.getUserId(),user, GlobalParam.tokenTime);
        }
        return Response.success(user);
    }

    public LoginUserInfo getCurrentUser(){
        if(!redisService.hasKey(ServletUtil.getToken())){
            return null;
        }
        return (LoginUserInfo)redisService.get(ServletUtil.getToken());
    }

    public Response getUserList() {
        List<JbdpEmployee> employeeList = employeeMapper.selectList(new LambdaQueryWrapper<JbdpEmployee>().eq(JbdpEmployee::getState, 1));
        return Response.success(Converter.INSTANCE.employee2UserDao(employeeList));
    }

    //根据指定账号获取人员信息
    public UserInfo getUserinfo(String account) {
        UserInfo userInfo = loginMapper.getUserinfo(account);
        return userInfo;
    }

    public Response superviseCode(String code) {
        return Response.success(loginMapper.superviseCode(code));
    }

    public Response linkcode(String supervisionTypeName) {
        return Response.success(loginMapper.getLinkCode(supervisionTypeName));
    }

    public CodeParams getLinkName(String linkCode) {
        return loginMapper.getLinkName(linkCode);
    }

    //查询下一环节 是否存在  存在 1 不存在 0


    public static Integer isHaveNextStep(String linkCode) {
        if(StringUtils.isBlank(linkCode)) return 1;
        //string 转 long
        int value = Integer.valueOf(linkCode);
        value += 1;
        long count = Arrays.stream(GlobalParam.linkCodes).filter(v -> v == Integer.valueOf(linkCode) + 1).count();
        return Math.toIntExact(count);
    }

    @Autowired
    private SysUploadFileMapper uploadFileMapper;
    public Response getfile(String mainId, String mainType) {
        List<SysUploadFile> files = uploadFileMapper.selectList(new LambdaQueryWrapper<SysUploadFile>().eq(SysUploadFile::getMainId, mainId)
                .eq(SysUploadFile::getMainType, mainType).orderByDesc(SysUploadFile::getDoTime));
        return Response.success(files);
    }
}
